package com.yyq.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author yyq
 * @create 2021-08-03 11:31
 */
//1 创建call方法的实现类
public class NewThread implements Callable {
    @Override
    //重写call方法
    public Object call() throws Exception {
        int sum=0;
        for (int i = 0; i < 100; i++) {
            sum+=i;
        }
        Integer ssum=sum;
        return ssum;
    }
}



class test_02{
    public static void main(String[] args) {
        //2 创建Callable接口的实现类
        NewThread  t=new NewThread();
        //3 将Callable接口的实现类的对象 传递到FutureTask构造器中，创建FutureTask对象
        FutureTask futureTask = new FutureTask(t);
        //4 将FutureTask对象作为参数传递到Thread类的构造器中，创建Thread对象，并调用Start方法
        new Thread(futureTask).start();
        try {
            //通过get方法获取  重写call方法的返回值
            Object o = futureTask.get();
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}